// pages/my/familyManage.js
const app = getApp();
var topOpacity = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "家人管理", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: true, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    showMonment: true, //是否显示须知
    name: '',

    authData: {
      authType: '0', //0表示使用登录弹窗  1表示使用完善昵称头像弹窗 2表示直接弹出头像昵称选择框
      authUsername: "false", //是否已经授权头像昵称
    },
    showLogin: false, //是否显示授权弹窗
    showWsInfoDom: false, //直接显示头像昵称弹窗
    showPrivacyFlag: app.globalData.showPrivacyFlag,

    list: [],
    houseInfo: '',
    openid: '',
    shareTitle: '',
    shareImg: '',
    mySf: '',
    is_register: 0,
    familyId: 0,
    billId: '', //发起邀请的邀请id
    is_nickAndHead: 0,
    myUserId: ''
  },
  onPageScroll(t) {
    if (t.scrollTop < 40) {
      topOpacity = 0
      if (this.data.topOpacity != 0) {
        this.setData({
          topOpacity: 0
        })
      }
    } else if (t.scrollTop >= 40) {
      if (topOpacity <= 1) {
        topOpacity = t.scrollTop / 100 - .4
      } else {
        topOpacity = 1
      }
      if (topOpacity <= 1) {
        this.setData({
          topOpacity: topOpacity
        })
      }
    }
  },
  //点击我知晓了
  closeMonment() {
    let that = this;
    that.setData({
      showMonment: false
    })
  },
  // 姓名
  nameIpt(e) {
    this.setData({
      name: e.detail.value
    })
  },

  //处理完善信息--包含隐私协议
  addWxInfo() {
    this.setData({
      ['authData.authType']: 1,
      showLogin: true
    });
  },

  //接收子组件电话授权及头像昵称授权后后返回的值
  returnVal(e) {
    var that = this;
    var list = that.data.list;
    list[0].photo = that.data.baseUrl + e.detail.head;
    list[0].nickName = e.detail.nickname
    //授权完头像昵称
    this.setData({
      is_nickAndHead: e.detail.is_nickAndHead,
      list: list
    });

    app.showTip('个人信息已完善');
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;

    //获取缓存中的openid
    var openid = wx.getStorageSync('ly24_openid');

    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function(res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function(res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getMyFamilyPageInfo(res.data.openid);
              },
              fail: function(res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getMyFamilyPageInfo(openid);
    }

    //调用分享
    that.getShareInfo();

  },

  //获取本业数据
  getMyFamilyPageInfo(openid) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...'
    })
    wx.request({
      url: that.data.baseUrl + 'getMyFamilyPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //如果我的身份不是业主
          // if (res.data.mySf != '业主') {
          //   app.showTip('您当前不是业主身份哦');
          //   setTimeout(function() {
          //     wx.navigateBack({
          //       delta: 1
          //     });
          //   }, 1500);
          //   return;
          // }

          //重新赋值后台请求数据
          that.setData({
            list: res.data.list,
            houseInfo: res.data.houseInfo,
            mySf: res.data.mySf,
            is_register: res.data.is_register,
            familyId: res.data.familyId,
            billId: res.data.billId,
            is_nickAndHead: res.data.is_nickAndHead,
            myUserId: res.data.myUserID
          })
        } else {
          app.showTip(res.data.msg);
          setTimeout(function() {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function() {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function() {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },


  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function(res) {}
    });
  },

  //删除家人
  delUser(e) {
    var that = this;

    //判断我当前的身份
    if (that.data.mySf != '业主') {
      app.showTip('您当前身份不是业主,无法解除绑定关系哦');
      return;
    }
    wx.showModal({
      title: '温馨提示',
      content: '您确认移除此家人吗?',
      complete: (res) => {
        if (res.confirm) {
          var userId = e.currentTarget.dataset.userid;
          var familyId = that.data.familyId;
          var index = e.currentTarget.dataset.index;

          wx.showLoading({
            title: '绑定关系解除中...'
          })
          wx.request({
            url: that.data.baseUrl + 'delFamilyUser.php',
            method: 'POST',
            data: {
              'openid': that.data.openid,
              'userId': userId,
              'familyId': familyId
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
              wx.hideLoading();
              app.showTip(res.data.msg);
              if (res.data.status == 1) {
                //重新赋值后台请求数据
                var list = that.data.list;
                //删除数组中的元素
                list.splice(index, 1);
                that.setData({
                  list: list
                });
              }
            },
            fail: function() {
              wx.hideLoading();
              app.showTip('抱歉,服务器繁忙');
            }
          });
        }
      }
    })
  },

  //刷新billId 分享一次刷新一次
  fleshBillId() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'fleshBillId.php',
      method: 'POST',
      data: {
        'userId': that.data.myUserId,
        'familyId': that.data.familyId
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function(res) {
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            billId: res.data.billId
          });
        }
      },
      fail: function() {

      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    //刷新邀请id
    if (that.data.mySf == '业主') {
      that.fleshBillId();
    }
    return {
      title: that.data.shareTitle,
      path: '/pages/my/familyInvite?familyId=' + that.data.familyId + "&shareOpenid=" + that.data.openid + '&billId=' + that.data.billId,
      imageUrl: that.data.shareImg,
      success: function(res) {

      },
      fail: function(res) {

      }
    }
  }
})